package gui.views;

import gui.LibraryList;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.prefs.Preferences;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import libraries.Library;

public class SelectLibraryDialog extends JDialog {
    
    public static Library show(Frame owner)
    {
        SelectLibraryDialog dialog = new SelectLibraryDialog(owner);
        dialog.setVisible(true);
        Library library = dialog.getSelectedLibrary();
        dialog.dispose();
        return library;
    }
    
    private final LibraryList libraryList;
    
    public SelectLibraryDialog(Frame owner)
    {
        super(owner, "Select Library", true);
        setLayout(new BorderLayout());
        
        libraryList = new LibraryList();
        libraryList.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                if(SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
                    setVisible(false);
                }
            }
        });
        add(new JScrollPane(libraryList), BorderLayout.WEST);
        // TODO Auto-generated constructor stub
        add(createButtonPanel(), BorderLayout.SOUTH);
        
        pack();
        setMinimumSize(getSize());
        
        Preferences prefs = Preferences.userNodeForPackage(SelectLibraryDialog.class);
        int width = prefs.getInt("width", getWidth());
        int height = prefs.getInt("height", getHeight());
        setSize(width, height);
    }
    
    public Library getSelectedLibrary()
    {
        if(libraryList.getSelectedValue() == null) {
            return null;
        }
        return libraryList.getSelectedValue().getLibrary();
    }
    
    private JPanel createButtonPanel()
    {
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.TRAILING));
        
        JButton okButton = new JButton();
        okButton.setAction(new AbstractAction("Create") {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                setVisible(false);
            }
        });
        buttonPanel.add(okButton);
        
        JButton cancelButton = new JButton();
        cancelButton.setAction(new AbstractAction("Cancel") {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                //TODO
                libraryList.setSelectedIndex( -1);
                setVisible(false);
            }
        });
        buttonPanel.add(cancelButton);
        return buttonPanel;
    }
    
    @Override
    public void dispose()
    {
        Preferences prefs = Preferences.userNodeForPackage(SelectLibraryDialog.class);
        prefs.putInt("width", getWidth());
        prefs.putInt("height", getHeight());
        super.dispose();
    }
}
